
#################################################
####### Replication code for "The Connections of Party Brokers," Journal of Politics
####### This file includes replication code for all analyses in the ONLINE APPENDIX
###################################################

rm(list=ls())
library(foreign)
library(MASS)
library(tidyverse)
library(psycho)
library(margins)
library(dotwhisker) 
library(broom) 
library(xtable) 
library(stargazer)
library(apsrtable)


###################################
###### CALL IN DATA
###################################

load("BrierleyNathan_brokers.Rdata")

#Contains the following 4 objects:
#d: individual-level survey data. row = aspiring branch leader in 2018
#bl: d collapsed to the Branch Level ("bl"). row = polling station. variables such as connections up/down are the average across the incumbent branch leaders
#ps: d collapsed to the Position Level ("ps"). row = all contestants, for example, for secretary within branch X. variables such as connections up/down are for the incumbent for that position
#elections: is d subset to participants in actual contested elections for positions

#d.incrun: d subset only to incumbent branch executives who are recontesting (for use in subsequent analyses below)
d.incrun <- dplyr::filter(d, old_branch_position==1 & run_same_pos==1)
dim(d.incrun)


######################################################################
######################################################################
### CODE FOLLOWS ORDER OF PRESENTATION IN MAIN TEXT
######################################################################
######################################################################

###################################
###### Table SI.2: 
###################################

## See lines 97-101 of maintext replication. 

###################################
###### Table SI.3: regressions of correlates of connections down  
###################################

reg_1 <- lm(cxn_down_percentage_correct_full ~ farming_households_2km_p, data=d )
reg_2 <- lm(cxn_down_percentage_correct_full ~ closest_ps_km.x, data=d )
reg_3 <- lm(cxn_down_percentage_correct_full ~ lives_outside_ps, data=d )
reg_4 <- lm(cxn_down_percentage_correct_full ~ chief_relative, data=d )
reg_5 <- lm(cxn_down_percentage_correct_full ~ years_comm, data=d )
reg_6 <- lm(cxn_down_percentage_correct_full ~ ethfrac_upperlevel_2km , data=d )
reg_7 <- lm(cxn_down_percentage_correct_full ~ urban , data=d )
reg_8 <- lm(cxn_down_percentage_correct_full ~ wealth_index_2km , data=d )
reg_9 <- lm(cxn_down_percentage_correct_full ~ TOTAL.VOTERS.x, data=d )
reg_10 <- lm(cxn_down_percentage_correct_full ~ TOTAL.VOTERS.x +wealth_index_2km + urban +ethfrac_upperlevel_2km + years_comm + 
chief_relative + lives_outside_ps + closest_ps_km.x + farming_households_2km_p, data=d) 

stargazer(reg_1, reg_2, reg_3, reg_4, reg_5, reg_6, reg_7, reg_8, reg_9, reg_10)

###################################
###### Table SI.4: Association between connections down and individual- and community-level variables 
###################################

reg_comm <- lm(cxn_down_percentage_correct_full~  TOTAL.VOTERS.x + closest_ps_km.x + farming_households_2km_p, data=d )
summary(reg_comm)

reg_indiv <- lm(cxn_down_percentage_correct_full ~ log(age) + female + aspires_higherposition + local_eth_minority  + as.factor(bio_educ_three) + asset_index + years_comm + lives_outside_ps + petty_trader + farmer + chief_relative, data=d) 
summary(reg_indiv)

stargazer(reg_comm, reg_indiv)

###################################
#### Tables SI.6 and SI.5 see Maintext replication.r ####
###################################


###################################
###### Table SI.7: Vote swings and average broker connections  
###################################


m1 <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + npp12to16_const_swing, data=bl)
summary(m1)
m2 <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + as.factor(constituency), data=bl)
summary(m2)

stargazer(m1, m2)

###################################
###### Table SI.8: Repeating Table SI.7: polling stations with most limited population turnover  
###################################

summary(d$born_thistown_2km_p[d$urban==0])
summary(d$born_thistown_2km_p[d$urban==1])

m1R <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + npp12to16_const_swing, data=bl[bl$urban==0,])
summary(m1R)

m1R2 <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + npp12to16_const_swing, data=bl[bl$born_thistown_2km_p >= (2/3),])
summary(m1R2)

m2R <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + as.factor(constituency), data=bl[bl$urban==0,])
summary(m2R)

m2R2 <- lm(npp12to16_ps_swing.NEW ~ cxn_down_percentage_correct_full + cxn_up_percentage_correct_full  + as.factor(constituency), data=bl[bl$born_thistown_2km_p >= (2/3),])
summary(m2R2)

stargazer(m1R, m1R2, m2R, m2R2)

###################################
###### Figure SI.2: Predicted probability of winning contested election by connections up or down 
###################################


#model:
m1.psfe.nocontrol <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full  + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.nocontrol)

#make the relevant dummies:
elections$PS10.youth_org   <- ifelse(elections$branchposition.anon=="PS10-youth_org", 1, 0)
elections$PS104.org1   <- ifelse(elections$branchposition.anon=="PS104-org1", 1, 0)
elections$PS104.org2   <- ifelse(elections$branchposition.anon=="PS104-org2", 1, 0)
elections$PS104.sec1   <- ifelse(elections$branchposition.anon=="PS104-sec1", 1, 0)
elections$PS110.youth_org   <- ifelse(elections$branchposition.anon=="PS110-youth_org", 1, 0)
elections$PS111.chair   <- ifelse(elections$branchposition.anon=="PS111-chair", 1, 0)
elections$PS111.women_org   <- ifelse(elections$branchposition.anon=="PS111-women_org", 1, 0)
elections$PS13.women_org   <- ifelse(elections$branchposition.anon=="PS13-women_org", 1, 0)
elections$PS141.chair   <- ifelse(elections$branchposition.anon=="PS141-chair", 1, 0)
elections$PS141.org   <- ifelse(elections$branchposition.anon=="PS141-org", 1, 0)
elections$PS141.secretary   <- ifelse(elections$branchposition.anon=="PS141-secretary", 1, 0)
elections$PS141.women_org   <- ifelse(elections$branchposition.anon=="PS141-women_org", 1, 0)
elections$PS141.youth_org   <- ifelse(elections$branchposition.anon=="PS141-youth_org", 1, 0)
elections$PS146.chair   <- ifelse(elections$branchposition.anon=="PS146-chair", 1, 0)
elections$PS146.women_org   <- ifelse(elections$branchposition.anon=="PS146-women_org", 1, 0)
elections$PS147.women_org   <- ifelse(elections$branchposition.anon=="PS147-women_org", 1, 0)
elections$PS15.chair   <- ifelse(elections$branchposition.anon=="PS15-chair", 1, 0)
elections$PS15.secretary   <- ifelse(elections$branchposition.anon=="PS15-secretary", 1, 0)
elections$PS15.youth_org   <- ifelse(elections$branchposition.anon=="PS15-youth_org", 1, 0)
elections$PS153.womenorg1   <- ifelse(elections$branchposition.anon=="PS153-womenorg1", 1, 0)
elections$PS156.chair   <- ifelse(elections$branchposition.anon=="PS156-chair", 1, 0)
elections$PS157.youth_org   <- ifelse(elections$branchposition.anon=="PS157-youth_org", 1, 0)
elections$PS16.chair   <- ifelse(elections$branchposition.anon=="PS16-chair", 1, 0)
elections$PS16.org   <- ifelse(elections$branchposition.anon=="PS16-org", 1, 0)
elections$PS21.women_org   <- ifelse(elections$branchposition.anon=="PS21-women_org", 1, 0)
elections$PS22.chair1   <- ifelse(elections$branchposition.anon=="PS22-chair1", 1, 0)
elections$PS22.org2   <- ifelse(elections$branchposition.anon=="PS22-org2", 1, 0)
elections$PS24.org   <- ifelse(elections$branchposition.anon=="PS24-org", 1, 0)
elections$PS25.org   <- ifelse(elections$branchposition.anon=="PS25-org", 1, 0)
elections$PS25.secretary   <- ifelse(elections$branchposition.anon=="PS25-secretary", 1, 0)
elections$PS25.women_org   <- ifelse(elections$branchposition.anon=="PS25-women_org", 1, 0)
elections$PS26.chair   <- ifelse(elections$branchposition.anon=="PS26-chair", 1, 0)
elections$PS26.secretary   <- ifelse(elections$branchposition.anon=="PS26-secretary", 1, 0)
elections$PS26.youth_org   <- ifelse(elections$branchposition.anon=="PS26-youth_org", 1, 0)
elections$PS27.chair   <- ifelse(elections$branchposition.anon=="PS27-chair", 1, 0)
elections$PS27.org   <- ifelse(elections$branchposition.anon=="PS27-org", 1, 0)
elections$PS27.youth_org   <- ifelse(elections$branchposition.anon=="PS27-youth_org", 1, 0)
elections$PS32.women_org   <- ifelse(elections$branchposition.anon=="PS32-women_org", 1, 0)
elections$PS33.chair   <- ifelse(elections$branchposition.anon=="PS33-chair", 1, 0)
elections$PS33.women_org   <- ifelse(elections$branchposition.anon=="PS33-women_org", 1, 0)
elections$PS34.chair   <- ifelse(elections$branchposition.anon=="PS34-chair", 1, 0)
elections$PS35.chair1   <- ifelse(elections$branchposition.anon=="PS35-chair1", 1, 0)
elections$PS37.org   <- ifelse(elections$branchposition.anon=="PS37-org", 1, 0)
elections$PS38.chair1   <- ifelse(elections$branchposition.anon=="PS38-chair1", 1, 0)
elections$PS38.org1   <- ifelse(elections$branchposition.anon=="PS38-org1", 1, 0)
elections$PS38.womenorg1   <- ifelse(elections$branchposition.anon=="PS38-womenorg1", 1, 0)
elections$PS40.women_org   <- ifelse(elections$branchposition.anon=="PS40-women_org", 1, 0)
elections$PS48.secretary   <- ifelse(elections$branchposition.anon=="PS48-secretary", 1, 0)
elections$PS5.org   <- ifelse(elections$branchposition.anon=="PS5-org", 1, 0)
elections$PS5.women_org   <- ifelse(elections$branchposition.anon=="PS5-women_org", 1, 0)
elections$PS56.womenorg1   <- ifelse(elections$branchposition.anon=="PS56-womenorg1", 1, 0)
elections$PS63.chair   <- ifelse(elections$branchposition.anon=="PS63-chair", 1, 0)
elections$PS63.secretary   <- ifelse(elections$branchposition.anon=="PS63-secretary", 1, 0)
elections$PS66.org   <- ifelse(elections$branchposition.anon=="PS66-org", 1, 0)
elections$PS7.women_org   <- ifelse(elections$branchposition.anon=="PS7-women_org", 1, 0)
elections$PS71.secretary   <- ifelse(elections$branchposition.anon=="PS71-secretary", 1, 0)
elections$PS72.women_org   <- ifelse(elections$branchposition.anon=="PS72-women_org", 1, 0)
elections$PS79.org   <- ifelse(elections$branchposition.anon=="PS79-org", 1, 0)
elections$PS90.org   <- ifelse(elections$branchposition.anon=="PS90-org", 1, 0)


elections.dat <- na.omit(elections[,c("won_seat", "cxn_down_percentage_correct_full" , "cxn_up_percentage_correct_full"  , "old_branch_position", "PS10.youth_org" ,                "PS104.org1"  , "PS104.org2"  , "PS104.sec1"        , "PS110.youth_org"   ,                   "PS111.chair"  ,"PS111.women_org"   ,   "PS13.women_org"     ,"PS141.chair" ,                "PS141.org" , "PS141.secretary"  , "PS141.women_org"   , "PS141.youth_org" ,                   "PS146.chair"      ,"PS146.women_org"    , "PS147.women_org"   , "PS15.chair"  ,          "PS15.secretary" , "PS15.youth_org"  ,"PS153.womenorg1" , "PS156.chair"  ,   "PS157.youth_org"   , "PS16.chair"   ,  "PS16.org"    ,"PS21.women_org"   ,  "PS22.chair1"  , "PS22.org2" , "PS24.org", "PS25.org"  , "PS25.secretary"   , "PS25.women_org"   ,                      "PS26.chair"     , "PS26.secretary"     , "PS26.youth_org"   ,"PS27.chair"  ,                      "PS27.org"       , "PS27.youth_org"   ,   "PS32.women_org"      ,"PS33.chair"    , "PS33.women_org"     ,"PS34.chair"     ,  "PS35.chair1"  , "PS37.org"   ,   "PS38.chair1"  ,  "PS38.org1"  ,  "PS38.womenorg1"   ,"PS40.women_org"  ,"PS48.secretary"    , "PS5.org" , "PS5.women_org"  ,"PS56.womenorg1",  "PS63.chair" , "PS63.secretary"   ,          "PS66.org" , "PS7.women_org"   ,"PS71.secretary" ,"PS72.women_org"  ,"PS79.org" ,"PS90.org"  )])


#PLOT PROBABILITY OF ELECTION ACROSS RANGE OF CXN UP
set.seed(48103)
vcovCL <- vcov(m1.psfe.nocontrol)
N <- 1000
betas <- mvrnorm(n = N, mu= m1.psfe.nocontrol $coefficients, vcovCL)

summary(elections.dat $cxn_up_percentage_correct_full)
var.seq <- seq(0, 0.56, by=0.01)

pred.all <- as.data.frame(matrix(NA, nrow=N, ncol=length(var.seq)))

for(i in 1:length(var.seq)){
	
xt <- cbind(1, elections.dat$cxn_down_percentage_correct_full,  var.seq[i], elections.dat$old_branch_position, elections.dat$PS10.youth_org ,  elections.dat$PS104.org1  , elections.dat$PS104.org2  , elections.dat$PS104.sec1  , elections.dat$PS110.youth_org , elections.dat$PS111.chair  ,elections.dat$PS111.women_org , elections.dat$PS13.women_org ,elections.dat$PS141.chair ,  elections.dat$PS141.org , elections.dat$PS141.secretary  , elections.dat$PS141.women_org , elections.dat$PS141.youth_org , elections.dat$PS146.chair  ,elections.dat$PS146.women_org  , elections.dat$PS147.women_org , elections.dat$PS15.chair  ,  elections.dat$PS15.secretary , elections.dat$PS15.youth_org  ,elections.dat$PS153.womenorg1 , elections.dat$PS156.chair  , elections.dat$PS157.youth_org , elections.dat$PS16.chair ,  elections.dat$PS16.org  ,elections.dat$PS21.women_org ,  elections.dat$PS22.chair1  , elections.dat$PS22.org2 , elections.dat$PS24.org, elections.dat$PS25.org  , elections.dat$PS25.secretary , elections.dat$PS25.women_org ,  elections.dat$PS26.chair , elections.dat$PS26.secretary , elections.dat$PS26.youth_org ,elections.dat$PS27.chair  ,  elections.dat$PS27.org , elections.dat$PS27.youth_org , elections.dat$PS32.women_org  ,elections.dat$PS33.chair  , elections.dat$PS33.women_org ,elections.dat$PS34.chair ,  elections.dat$PS35.chair1  , elections.dat$PS37.org , elections.dat$PS38.chair1  ,  elections.dat$PS38.org1  ,  elections.dat$PS38.womenorg1 ,elections.dat$PS40.women_org  ,elections.dat$PS48.secretary  , elections.dat$PS5.org , elections.dat$PS5.women_org  ,elections.dat$PS56.womenorg1,  elections.dat$PS63.chair , elections.dat$PS63.secretary ,  elections.dat$PS66.org , elections.dat$PS7.women_org ,elections.dat$PS71.secretary ,elections.dat$PS72.women_org  ,elections.dat$PS79.org ,elections.dat$PS90.org)
tprob<-apply((1/(1+exp(-(as.matrix(xt))%*% t(betas)))),1,as.vector)
tprob.means <- apply(tprob, 1, mean)

pred.all[,i] <- tprob.means

}

means <- apply(pred.all, 2, mean)
lowCI <- c()
highCI <- c()
for(j in 1:ncol(pred.all)){
lowCI[j] <- quantile(pred.all[,j], probs=c(0.025))
highCI[j] <- quantile(pred.all[,j], probs=c(0.975))
}


### PULL OUT STATISICS FOR THE TEXT
quantile(elections.dat $cxn_up_percentage_correct_full, probs=c(0.1, 0.9), na.rm=T)
 # 10%  90% 
# 0.04 0.36 

var.seq[5]
var.seq[37]

mean(pred.all[,37] - pred.all[,5])
 #  0.2690088
quantile(pred.all[,37] - pred.all[,5], c(0.025, 0.975))
      # 2.5%      97.5% 
# 0.01238143 0.46507513 


###### DO SAME SIMULATION FOR CONNECTIONS DOWN

set.seed(48103)
vcovCL <- vcov(m1.psfe.nocontrol)
N <- 1000
betas <- mvrnorm(n = N, mu= m1.psfe.nocontrol $coefficients, vcovCL)

summary(elections.dat $cxn_down_percentage_correct_full)
var.seq2 <- seq(0, 0.9167, by=0.01)

pred.all <- as.data.frame(matrix(NA, nrow=N, ncol=length(var.seq)))

for(i in 1:length(var.seq2)){
	
xt <- cbind(1, var.seq2[i], elections.dat$cxn_up_percentage_correct_full,  elections.dat$old_branch_position, elections.dat$PS10.youth_org ,  elections.dat$PS104.org1  , elections.dat$PS104.org2  , elections.dat$PS104.sec1  , elections.dat$PS110.youth_org , elections.dat$PS111.chair  ,elections.dat$PS111.women_org , elections.dat$PS13.women_org ,elections.dat$PS141.chair ,  elections.dat$PS141.org , elections.dat$PS141.secretary  , elections.dat$PS141.women_org , elections.dat$PS141.youth_org , elections.dat$PS146.chair  ,elections.dat$PS146.women_org  , elections.dat$PS147.women_org , elections.dat$PS15.chair  ,  elections.dat$PS15.secretary , elections.dat$PS15.youth_org  ,elections.dat$PS153.womenorg1 , elections.dat$PS156.chair  , elections.dat$PS157.youth_org , elections.dat$PS16.chair ,  elections.dat$PS16.org  ,elections.dat$PS21.women_org ,  elections.dat$PS22.chair1  , elections.dat$PS22.org2 , elections.dat$PS24.org, elections.dat$PS25.org  , elections.dat$PS25.secretary , elections.dat$PS25.women_org ,  elections.dat$PS26.chair , elections.dat$PS26.secretary , elections.dat$PS26.youth_org ,elections.dat$PS27.chair  ,  elections.dat$PS27.org , elections.dat$PS27.youth_org , elections.dat$PS32.women_org  ,elections.dat$PS33.chair  , elections.dat$PS33.women_org ,elections.dat$PS34.chair ,  elections.dat$PS35.chair1  , elections.dat$PS37.org , elections.dat$PS38.chair1  ,  elections.dat$PS38.org1  ,  elections.dat$PS38.womenorg1 ,elections.dat$PS40.women_org  ,elections.dat$PS48.secretary  , elections.dat$PS5.org , elections.dat$PS5.women_org  ,elections.dat$PS56.womenorg1,  elections.dat$PS63.chair , elections.dat$PS63.secretary ,  elections.dat$PS66.org , elections.dat$PS7.women_org ,elections.dat$PS71.secretary ,elections.dat$PS72.women_org  ,elections.dat$PS79.org ,elections.dat$PS90.org)
tprob<-apply((1/(1+exp(-(as.matrix(xt))%*% t(betas)))),1,as.vector)
tprob.means <- apply(tprob, 1, mean)

pred.all[,i] <- tprob.means

}

means2 <- apply(pred.all, 2, mean)
lowCI2 <- c()
highCI2 <- c()
for(j in 1:ncol(pred.all)){
lowCI2[j] <- quantile(pred.all[,j], probs=c(0.025))
highCI2[j] <- quantile(pred.all[,j], probs=c(0.975))
}


### PULL OUT QUOTABLE STATISICS FOR THE TEXT
quantile(elections.dat $cxn_down_percentage_correct_full, probs=c(0.1, 0.9), na.rm=T)
      # 10%       90% 
# 0.0000000 0.6083333 

var.seq2[1]
var.seq2[62]

mean(pred.all[,62] - pred.all[,1])
 #0.09039768
quantile(pred.all[,62] - pred.all[,1], c(0.025, 0.975))
      # 2.5%      97.5% 
# -0.3068958  0.4680300 


#COMBINED FIGURE
par(mfrow=c(1,2), oma=c(0,0,3,0))

plot(var.seq2, means2, xlab="Connections Down of Aspirant (Proportion)", ylab="Predicted Probability of Winning Election", ylim=c(0, max(highCI)+0.02), pch=16, cex=1.1, col="white")
segments(var.seq2, lowCI2, var.seq2, highCI2, lty="solid", lwd=0.5, col="grey33")
points(var.seq2, means2,  pch=16, cex=1.1, col="dodgerblue3")
#abline(h=0, col="firebrick")
rug(jitter(elections.dat$cxn_down_percentage_correct_full))

plot(var.seq, means, xlab="Connections Up of Aspirant (Proportion)", ylab="Predicted Probability of Winning Election", ylim=c(0, max(highCI)+0.02), pch=16, cex=1.1, col="white")
segments(var.seq, lowCI, var.seq, highCI, lty="solid", lwd=0.5, col="grey33")
points(var.seq, means,  pch=16, cex=1.1, col="dodgerblue3")
#abline(h=0, col="firebrick")
rug(jitter(elections.dat$cxn_up_percentage_correct_full))

title("Predicted Probability of Winning Contested Election\n(n=60 contests)", outer=TRUE)





###################################
###### Table SI.9: Model from Figure SI.2, with predictors of connections up added 
###################################


#put in one by one: does it still survive? 
m1.psfe.control1 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + female   + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control1)

m1.psfe.control2 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + local_eth_minority   + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control2)

m1.psfe.control3 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + as.factor(bio_educ_three)  + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control3)

m1.psfe.control4 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + asset_index + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control4)

m1.psfe.control5 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + civil_servant + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control5)

m1.psfe.control6 <- glm(won_seat ~  cxn_down_percentage_correct_full +  cxn_up_percentage_correct_full + aspires_higherposition + old_branch_position + as.factor(branchposition.anon), data=elections, family="binomial")
summary(m1.psfe.control6)



###################################
###### Table SI.10: Table 4, with total and unique connections 
###################################

m1 <- glm(all_acclaimed ~ total_down  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl, family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ total_down  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl, family="binomial")
summary(m1b)

ps2 <- ps[is.na(ps$respid)==F,]
dim(ps2)
ps2a <- merge(ps2, d[,c("respid", "cxn_down_percentage_correct_full_weighted")], by="respid", all.x=T, all.y=F)
dim(ps2a)


m3 <- glm(multiple_aspirants ~ cxn_down_percentage_correct_full_weighted + cxn_up_percentage_correct_full + incumbent_is_contesting + branch_is_active.1 +wealth_index_2km+ethfrac_upperlevel_2km+npp2016_pres_p.NEW + female + log(age) + as.factor(bio_educ_three) + asset_index + as.factor(position.fac) + as.factor(constituency), data= ps2a, family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants ~ cxn_down_percentage_correct_full_weighted + cxn_up_politicians + cxn_up_bureaucrats  + cxn_up_constexecs + incumbent_is_contesting + branch_is_active.1 + wealth_index_2km + ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW + female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps2a, family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ cxn_down_percentage_correct_full_weighted  +  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs  +  incumbent_is_contesting, data= ps2a[ps2a$multiple_aspirants==1,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ cxn_down_percentage_correct_full_weighted +  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs  + incumbent_is_contesting + branch_is_active.1 + wealth_index_2km  + ethfrac_upperlevel_2km + npp2016_pres_p.NEW + as.factor(position.fac), data= ps2a[ps2a$multiple_aspirants==1,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ cxn_down_percentage_correct_full_weighted 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun[d.incrun $multiple_aspirants==1,], family="binomial")
summary(m6)

###################################
###### Table SI.11: Table 4, with ties to Akan voters only  
###################################


m1 <- glm(all_acclaimed ~ faces_akan_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl, family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ faces_akan_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl, family="binomial")
summary(m1b)

m3 <- glm(multiple_aspirants ~ 
faces_akan_percentage_correct_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps, family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants ~ 
faces_akan_percentage_correct_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps, family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ faces_akan_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps[ps$multiple_aspirants==1,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ faces_akan_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps[ps$multiple_aspirants==1,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ faces_akan_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun[d.incrun$multiple_aspirants==1,], family="binomial")
summary(m6)



###################################
###### Table SI.12: Table 4, with ties to young voters only  
###################################


#### merge to the PS object:

ps2 <- ps[is.na(ps$respid)==F,]
dim(ps2)
ps2a <- merge(ps2, d[,c("respid", "faces_potential_familyhead_full", "faces_youth_full")], by="respid", all.x=T, all.y=F)
dim(ps2a)


#### collapse to the bl object

holder <- as.data.frame(matrix(NA, nrow=nrow(bl), ncol=3))
colnames(holder) <- c("anon.ps.name", "faces_potential_familyhead_full", "faces_youth_full")
for(i in 1:length(unique(bl$anon.ps.name))){

holder[i,1] <- 	unique(bl$anon.ps.name)[i]
d.new <- d[d$anon.ps.name == unique(bl$anon.ps.name)[i], c("faces_potential_familyhead_full", "faces_youth_full")]
holder[i,2] <- mean(d.new$faces_potential_familyhead_full, na.rm=T)	
holder[i,3] <- mean(d.new$faces_youth_full, na.rm=T)	

}

dim(bl)
bl2 <- merge(bl, holder, by="anon.ps.name", all.x=T, all.y=F)
dim(bl2)


d.incrun2 <- dplyr::filter(d, old_branch_position==1 & run_same_pos==1)
dim(d.incrun2)

#MODELS FROM HERE:
m1 <- glm(all_acclaimed ~ faces_youth_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl2, family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ faces_youth_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl2, family="binomial")
summary(m1b)

m3 <- glm(multiple_aspirants~
faces_youth_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps2a, family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants~
faces_youth_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW 
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps2a, family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ faces_youth_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps2a[ps2a $multiple_aspirants==1,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ faces_youth_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps2a[ps2a $multiple_aspirants==1,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ faces_youth_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun2[d.incrun2 $multiple_aspirants==1,], family="binomial")
summary(m6)



###################################
###### Table SI.13: Table 4, with ties to likely family heads only  
###################################

m1 <- glm(all_acclaimed ~ faces_potential_familyhead_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl2, family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ faces_potential_familyhead_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl2, family="binomial")
summary(m1b)

m3 <- glm(multiple_aspirants~
faces_potential_familyhead_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW 
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps2a, family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants~
faces_potential_familyhead_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps2a, family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ faces_potential_familyhead_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps2a[ps2a $multiple_aspirants==1,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ faces_potential_familyhead_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps2a[ps2a $multiple_aspirants==1,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ faces_potential_familyhead_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun2[d.incrun2 $multiple_aspirants==1,], family="binomial")
summary(m6)



###################################
###### Table SI.14: Table 4, competitive polling stations only  
###################################

m1 <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$pres2016_ps_margin.NEW < 0.2,], family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$pres2016_ps_margin.NEW < 0.2,], family="binomial")
summary(m1b)

m3 <-glm(multiple_aspirants~
cxn_down_percentage_correct_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps[ps$pres2016_ps_margin.NEW < 0.2, ], family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants~
cxn_down_percentage_correct_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW 
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps[ps$pres2016_ps_margin.NEW < 0.2, ], family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps[ps$multiple_aspirants==1 & ps$pres2016_ps_margin.NEW < 0.2,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps[ps$multiple_aspirants==1 & ps$pres2016_ps_margin.NEW < 0.2,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun[d.incrun $multiple_aspirants==1 & d.incrun $pres2016_ps_margin.NEW < 0.2,], family="binomial")
summary(m6)


###################################
###### Table SI.15: Table 4, urban polling stations only 
###################################


m1 <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$urban==1,], family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$urban==1,], family="binomial")
summary(m1b)

m3 <- glm(multiple_aspirants ~
cxn_down_percentage_correct_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps[ps$urban==1,], family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants~
cxn_down_percentage_correct_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW 
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps[ps$urban==1,], family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps[ps$multiple_aspirants==1 & ps$urban==1,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps[ps$multiple_aspirants==1 & ps$urban==1,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun[d.incrun $multiple_aspirants==1 & d.incrun$urban==1,], family="binomial")
summary(m6)


###################################
###### Table SI.16: Table 4, polling stations where chiefs are not brokers 
###################################


table(bl$chief_reported_asbroker)
chiefs.not.brokers <- na.omit(bl$anon.ps.name[bl$chief_reported_asbroker== 0])
	#where no respondents reported that chief acts as a broker


m1 <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp2016_pres_p.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$anon.ps.name %in% chiefs.not.brokers,], family="binomial")
summary(m1)

m1b <- glm(all_acclaimed ~ cxn_down_percentage_correct_full  + cxn_up_percentage_correct_full  + closest_ps_km  + wealth_index_2km  + npp12to16_ps_swing.NEW  + ethfrac_upperlevel_2km  + branch_is_active  + as.factor(constituency), data=bl[bl$anon.ps.name %in% chiefs.not.brokers,], family="binomial")
summary(m1b)

m3 <- glm(multiple_aspirants~
cxn_down_percentage_correct_full 
+ cxn_up_percentage_correct_full 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), data= ps[ps$anon.ps.name %in% chiefs.not.brokers, ], family="binomial")
summary(m3)

m4 <- glm(multiple_aspirants ~
cxn_down_percentage_correct_full 
+ cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ female 
+ log(age) 
+ as.factor(bio_educ_three) 
+ asset_index 
+ as.factor(position.fac) 
+ as.factor(constituency), 
data= ps[ps$anon.ps.name %in% chiefs.not.brokers, ], family="binomial")
summary(m4)

m5.sparse <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting, data= ps[ps$multiple_aspirants==1 & ps$anon.ps.name %in% chiefs.not.brokers,], family="binomial")
summary(m5.sparse)

m5 <- glm(backroom_deal ~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+  incumbent_is_contesting 
+ branch_is_active.1
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(position.fac), data= ps[ps$multiple_aspirants==1 & ps$anon.ps.name %in% chiefs.not.brokers,], family="binomial")
summary(m5)

m6 <- glm(backroom_deal~ cxn_down_percentage_correct_full 
+  cxn_up_politicians  +  cxn_up_bureaucrats  +  cxn_up_constexecs 
+ branch_is_active.y
+ wealth_index_2km 
+ ethfrac_upperlevel_2km
+ npp2016_pres_p.NEW
+ as.factor(ps2018_contest_position), data= d.incrun[d.incrun $multiple_aspirants==1 & d.incrun$anon.ps.name %in% chiefs.not.brokers,], family="binomial")
summary(m6)


